package client.gui.workbox;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import stalker.Stalker;
import broadcaster.Broadcaster;
import client.Chat;
import client.network.ServiceRequestCenter;
import client.session.ReceiveDataSessionVisitor;
import client.session.ReceiveInvitationSessionVisitor;
import client.session.SessionType;
import data.Data;

public class WorkBoxModel extends Broadcaster {
  
  private final List<SessionType> sessions_;
  private long active_session_index_;
  
  public WorkBoxModel(ServiceRequestCenter servicer) {
    assert servicer != null;
    sessions_ = new ArrayList<SessionType>();
    active_session_index_ = 0;
    // set default session
    sessions_.add(new Chat(servicer));
  }
  
  public synchronized void receiveData(Stalker sender, Data data) {
    assert sender != null && data != null;
    for (SessionType session : sessions_) {
      session.accept(new ReceiveDataSessionVisitor(sender, data));
    }
  }
  
  public synchronized void receiveInvitation(Stalker inviter, Stalker location) {
    assert inviter != null && location != null;
    for (SessionType session : sessions_) {
      session.accept(new ReceiveInvitationSessionVisitor(inviter, location));
    }
  }
  
  public synchronized void setActiveSession(long index) {
    assert (index >= 0 && index < sessions_.size());
    active_session_index_ = index;
    fire();
  }
  
  public synchronized JPanel getDisplay() {
    return sessions_.get((int)active_session_index_).getDisplay();
  }
  
  public synchronized void cleanUp() {
    for (SessionType session : sessions_) {
      session.cleanUp();
    }
  }
  
}
